/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.dao;

import wargame.entity.*;
import connectionmanager.DB;
import java.util.ArrayList;
import java.util.ArrayList;
import wargame.entity.Notification;

/**
 *
 * @author Clarence
 */
public class NotificationDAO {

    public static Notification retrieveNotification(int notification_id) {
        String sql = "SELECT `notification_id`, `player_id`, `message`, `turn`, `unread` FROM `notification` WHERE notification_id = '" + notification_id + "'";
        DB db = new DB();
        db.connect();
        db.query(sql);
        Notification n = null;
        if (db.next()) {
            n = new Notification();
            n.setNotification_id(db.getInt("notification_id"));
            n.setPlayer_id(db.getInt("player_id"));
            n.setMessage(db.getString("message"));
            n.setTurn(db.getInt("turn"));
            n.setUnread(db.getInt("unread"));
        }
        db.close();
        return n;
    }

    public static int insertNotification(Notification n) {
        String sql = "INSERT INTO notification (`player_id`, `message`, `turn`, `unread`) VALUES ('" + n.getPlayer_id() + "', '" + n.getMessage() + "', '" + n.getTurn() + "', '" + n.getUnread() + "');";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        int lastInsertedId = db.getLastInsertedID();
        db.close();
        return lastInsertedId;
    }

    public static void updateNotification(Notification n) {
        String sql = "UPDATE notification SET `player_id` = '" + n.getPlayer_id() + "', `message` = '" + n.getMessage() + "', `turn` = '" + n.getTurn() + "', `unread` = '" + n.getUnread() + "' WHERE notification_id = '" + n.getNotification_id() + "';";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }

    public static void deleteNotification(int notification_id) {
        String sql = "DELETE FROM notification WHERE notification_id = '" + notification_id + "'";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }

    public static ArrayList<Notification> retrievePlayerNotification(int player_id) {
        String sql = "SELECT * FROM notification WHERE player_id=" + player_id + " OR player_id=0";
        DB db = new DB();
        db.connect();
        db.query(sql);
        ArrayList<Notification> notification_list = new ArrayList<Notification>();
        while (db.next()) {
            Notification n = new Notification();
            n.setNotification_id(db.getInt("notification_id"));
            n.setPlayer_id(db.getInt("player_id"));
            n.setMessage(db.getString("message"));
            n.setTurn(db.getInt("turn"));
            n.setUnread(db.getInt("unread"));
            notification_list.add(n);
        }
        db.close();
        return notification_list;
    }

    public static void updateToRead(int notification_id) {
        String sql = "UPDATE notification SET unread = 0 WHERE notification_id =" + notification_id;
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }

    public static void clear() {
        String sql = "TRUNCATE TABLE notification";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }
}
